import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_study_2025/tag_flowwidget/test_demo.dart';

import 'httptest/login_test_page.dart';
import 'httptest/test_demo.dart';
import 'tag_flowwidget/tag_flow_demo.dart';
import 'tag_flowwidget/tese_demo2.dart';
import 'package:animate_do/animate_do.dart';

import 'twitter_profile_scroll/views/profile_view.dart';
import 'twitter_profile_scroll/const.dart';
import 'login/login_page1.dart';
import 'travel_concept/travel_concept_page.dart';
import 'aes/aes_demo_page.dart';

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo 集合',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: DemoListPage(),
      // 配置路由
      routes: {
        '/counter': (context) => const CounterDemoPage(),
        '/text': (context) => const TextDemoPage(),
        '/button': (context) => const ButtonDemoPage(),
        '/layout': (context) => const LayoutDemoPage(),
        '/animation': (context) => const AnimationDemoPage(),
        '/twitter_profile': (context) => TwitterProfileView(profile: profile),
        '/login': (context) => LoginPage1(),
        '/travel_concept': (context) => TravelConceptPage(),
        '/aes': (context) => AESDemoPage(),
        // '/login_test': (context) => const LoginTestPage(),
        '/login_test': (context) => LoginPage(),
      },
    );
  }
}

// Demo项目数据模型
class DemoItem {
  final String title;
  final String description;
  final String route;
  final IconData icon;

  DemoItem({
    required this.title,
    required this.description,
    required this.route,
    required this.icon,
  });
}

// 首页列表页面
class DemoListPage extends StatelessWidget {
  DemoListPage({super.key});

  // Demo项目列表
  final List<DemoItem> demoItems = [
    DemoItem(
      title: '计数器Demo',
      description: '经典的Flutter计数器示例',
      route: '/counter',
      icon: Icons.add_circle,
    ),
    DemoItem(
      title: '文本组件Demo',
      description: '各种文本样式和排版示例',
      route: '/text',
      icon: Icons.text_fields,
    ),
    DemoItem(
      title: '按钮组件Demo',
      description: '不同类型的按钮组件示例',
      route: '/button',
      icon: Icons.smart_button,
    ),
    DemoItem(
      title: '布局组件Demo',
      description: '常用布局组件的使用示例',
      route: '/layout',
      icon: Icons.view_quilt,
    ),
    DemoItem(
      title: '动画Demo',
      description: 'Flutter动画效果示例',
      route: '/animation',
      icon: Icons.animation,
    ),
    DemoItem(
      title: 'Twitter个人资料滚动',
      description: '仿Twitter个人资料页面的滚动效果',
      route: '/twitter_profile',
      icon: Icons.person,
    ),
    DemoItem(
      title: '登录页面Demo',
      description: '精美的登录界面设计，包含动画效果',
      route: '/login',
      icon: Icons.login,
    ),
    DemoItem(
      title: '旅行概念Demo',
      description: '精美的旅行应用界面，包含城市卡片滑动和详情页面',
      route: '/travel_concept',
      icon: Icons.travel_explore,
    ),
    DemoItem(
      title: 'AES加密解密Demo',
      description: 'AES-256 ECB模式加密解密，只需密钥无需IV',
      route: '/aes',
      icon: Icons.security,
    ),
    DemoItem(
      title: '圆形列表滚动',
      description: '3D圆形滚动列表效果，展示龙珠Z角色',
      route: '/circular_list',
      icon: Icons.view_carousel,
    ),
    DemoItem(
      title: '自定义折叠流布局实现',
      description: '3D圆形滚动列表效果，展示龙珠Z角色',
      route: '/tag_flow',
      icon: Icons.view_timeline_sharp,
    ),
    DemoItem(
      title: "登录接口测试",
      description: "测试HTTP登录API接口调用",
      route: "/login_test",
      icon: Icons.http,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo 集合'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        centerTitle: true,
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(16.0),
        itemCount: demoItems.length,
        itemBuilder: (context, index) {
          final item = demoItems[index];
          return Card(
            margin: const EdgeInsets.only(bottom: 12.0),
            elevation: 2,
            child: ListTile(
              leading: CircleAvatar(
                backgroundColor: Theme.of(context).colorScheme.primary,
                child: Icon(item.icon, color: Colors.white),
              ),
              title: Text(
                item.title,
                style: const TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              subtitle: Text(
                item.description,
                style: TextStyle(color: Colors.grey[600], fontSize: 14),
              ),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: () {
                Navigator.pushNamed(context, item.route);
              },
            ),
          );
        },
      ),
    );
  }
}

// 计数器Demo页面
class CounterDemoPage extends StatefulWidget {
  const CounterDemoPage({super.key});

  @override
  State<CounterDemoPage> createState() => _CounterDemoPageState();
}

class _CounterDemoPageState extends State<CounterDemoPage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('计数器Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('你已经点击按钮这么多次:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加',
        child: const Icon(Icons.add),
      ),
    );
  }
}

// 文本Demo页面
class TextDemoPage extends StatelessWidget {
  const TextDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('文本组件Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: const Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '这是标题文本',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 16),
            Text('这是普通文本，展示默认样式。', style: TextStyle(fontSize: 16)),
            SizedBox(height: 16),
            Text(
              '这是彩色文本',
              style: TextStyle(
                fontSize: 18,
                color: Colors.blue,
                fontWeight: FontWeight.w500,
              ),
            ),
            SizedBox(height: 16),
            Text(
              '这是斜体文本',
              style: TextStyle(fontSize: 16, fontStyle: FontStyle.italic),
            ),
          ],
        ),
      ),
    );
  }
}

// 按钮Demo页面
class ButtonDemoPage extends StatelessWidget {
  const ButtonDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('按钮组件Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            ElevatedButton(
              onPressed: () {},
              child: const Text('ElevatedButton'),
            ),
            const SizedBox(height: 16),
            OutlinedButton(
              onPressed: () {},
              child: const Text('OutlinedButton'),
            ),
            const SizedBox(height: 16),
            TextButton(onPressed: () {}, child: const Text('TextButton')),
            const SizedBox(height: 16),
            IconButton(onPressed: () {}, icon: const Icon(Icons.favorite)),
          ],
        ),
      ),
    );
  }
}

// 布局Demo页面
class LayoutDemoPage extends StatelessWidget {
  const LayoutDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('布局组件Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  width: 80,
                  height: 80,
                  color: Colors.red,
                  child: Center(child: Text('1')),
                ),
                Container(
                  width: 80,
                  height: 80,
                  color: Colors.green,
                  child: Center(child: Text('2')),
                ),
                Container(
                  width: 80,
                  height: 80,
                  color: Colors.blue,
                  child: Center(child: Text('3')),
                ),
              ],
            ),
            SizedBox(height: 20),
            Expanded(
              child: GridView.count(
                crossAxisCount: 2,
                children: [
                  Card(child: Center(child: Text('Grid 1'))),
                  Card(child: Center(child: Text('Grid 2'))),
                  Card(child: Center(child: Text('Grid 3'))),
                  Card(child: Center(child: Text('Grid 4'))),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// 动画Demo页面
class AnimationDemoPage extends StatefulWidget {
  const AnimationDemoPage({super.key});

  @override
  State<AnimationDemoPage> createState() => _AnimationDemoPageState();
}

class _AnimationDemoPageState extends State<AnimationDemoPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );
    _animation = Tween<double>(begin: 0, end: 1).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('动画Demo'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedBuilder(
              animation: _animation,
              builder: (context, child) {
                return Transform.rotate(
                  angle: _animation.value * 2 * 3.14159,
                  child: Container(
                    width: 100,
                    height: 100,
                    decoration: const BoxDecoration(
                      color: Colors.blue,
                      shape: BoxShape.circle,
                    ),
                    child: const Icon(
                      Icons.star,
                      color: Colors.white,
                      size: 50,
                    ),
                  ),
                );
              },
            ),
            const SizedBox(height: 32),
            ElevatedButton(
              onPressed: () {
                if (_controller.isAnimating) {
                  _controller.stop();
                } else {
                  _controller.repeat();
                }
              },
              child: const Text('开始/停止动画'),
            ),
          ],
        ),
      ),
    );
  }
}
